﻿Namespace form
    Public Class MenuBase

        Public Sub New()

            ' 此调用是 Windows 窗体设计器所必需的。
            InitializeComponent()

            ' 在 InitializeComponent() 调用之后添加任何初始化。

        End Sub

        Public Sub New(ByVal 菜单数据 As DataTable)

            ' 此调用是 Windows 窗体设计器所必需的。
            InitializeComponent()

            ' 在 InitializeComponent() 调用之后添加任何初始化。
            Me.菜单数据 = 菜单数据
        End Sub

        Public Sub New(ByVal 菜单数据 As DataTable, ByVal 菜单图标 As ImageList)

            ' 此调用是 Windows 窗体设计器所必需的。
            InitializeComponent()

            ' 在 InitializeComponent() 调用之后添加任何初始化。
            Me.菜单数据 = 菜单数据
            Me.菜单图标 = 菜单图标

        End Sub

#Region "属性"
        Private _菜单数据 As DataTable
        Public Property 菜单数据() As DataTable
            Get
                Return _菜单数据
            End Get
            Set(ByVal value As DataTable)
                _菜单数据 = value
            End Set
        End Property

        Private _菜单图标 As ImageList
        Public Property 菜单图标() As ImageList
            Get
                Return _菜单图标
            End Get
            Set(ByVal value As ImageList)
                _菜单图标 = value
            End Set
        End Property
#End Region

#Region "事件"
        Public Overridable Sub 加载菜单前()

        End Sub

        Public Overridable Sub 加载菜单后()

        End Sub

        Private Sub MenuBase_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            加载菜单前()

            加载菜单()

            加载菜单后()

        End Sub

        '编号 标题        DLL名称                  DLL 类                       父编号     图片Key 打开模式
        '1    工具        Wys.DataForm.CSharp.dll  Wys.DataForm.CSharp.Config   0代表顶级          0模态1非模态
        '2    工具子目录1 Wys.DataForm.CSharp.dll  Wys.DataForm.CSharp1.Config  1                                
        '3    工具子目录2 Wys.DataForm.CSharp.dll  Wys.DataForm.CSharp2.Config  1
        '4    工具子目录3 Wys.DataForm.CSharp.dll  Wys.DataForm.CSharp3.Config  1
        '5    工具子目录4 Wys.DataForm.CSharp.dll  Wys.DataForm.CSharp4.Config  1        
        Private Sub 加载菜单()

            If _菜单数据 Is Nothing OrElse _菜单数据.Rows.Count = 0 Then
                Exit Sub
            End If

            If 菜单图标 Is Nothing OrElse 菜单图标.Images.Count = 0 Then
                菜单图标 = New ImageList
                菜单图标.ImageSize = New Size(16, 16)
                菜单图标.Images.Add("Open", My.Resources.folder_blue_icon)
                菜单图标.Images.Add("Close", My.Resources.folder_close_blue_icon)
            End If

            tvMenu.ImageList = 菜单图标
            Dim drs() As DataRow = _菜单数据.Select("父编号=0")
            Dim nd As TreeNode
            For index As Integer = 0 To drs.Length - 1
                nd = tvMenu.Nodes.Add(CStr(drs(index)("标题")))
                nd.Name = CStr(drs(index)("编号"))
                nd.SelectedImageKey = CStr(drs(index)("图片Key"))
                加载菜单(nd)
            Next

        End Sub

        Private Sub 加载菜单(ByVal parentNode As TreeNode)

            Dim drs() As DataRow = _菜单数据.Select("父编号=" & parentNode.Name)
            Dim nd As TreeNode
            For index As Integer = 0 To drs.Length - 1
                nd = parentNode.Nodes.Add(CStr(drs(index)("标题")))
                nd.Name = CStr(drs(index)("编号"))
                nd.SelectedImageKey = CStr(drs(index)("图片Key"))
            Next

        End Sub
#End Region

#Region "共通方法"
        Public Shared Function GetEmptyMenuTable() As DataTable

            Dim dtMenu As New DataTable

            Dim dc As DataColumn = dtMenu.Columns.Add("编号", GetType(Integer))
            dc.DefaultValue = 0

            dc = dtMenu.Columns.Add("标题", GetType(String))
            dc.DefaultValue = ""

            dc = dtMenu.Columns.Add("DLL名称", GetType(String))
            dc.DefaultValue = ""

            dc = dtMenu.Columns.Add("DLL类", GetType(String))
            dc.DefaultValue = ""

            dc = dtMenu.Columns.Add("父编号", GetType(Integer))
            dc.DefaultValue = 0

            dc = dtMenu.Columns.Add("图片Key", GetType(String))
            dc.DefaultValue = "Open"

            dc = dtMenu.Columns.Add("打开模式", GetType(Integer))
            dc.DefaultValue = 0

            Return dtMenu

        End Function

#End Region

        Public Overridable Sub NodeMouseDoubleClickImpl(ByVal nd As TreeNode)

            Dim 编号 As Integer = nd.Name
            Dim drs() As DataRow = Me.菜单数据.Select("编号=" & 编号)

            If drs.Length = 0 Then
                Exit Sub
            End If

            Dim DLL名称 As String = CStr(drs(0)("DLL名称")).Trim
            Dim DLL类 As String = CStr(drs(0)("DLL类")).Trim
            Dim 打开模式 As Integer = CStr(drs(0)("打开模式")).Trim

            If DLL名称 = "" OrElse DLL类 = "" Then
                Exit Sub
            End If

            Dim assem As System.Reflection.Assembly = Nothing
            Dim bLoad As Boolean = True
            Try
                assem = System.Reflection.Assembly.LoadFrom(DLL名称)
            Catch ex As Exception
                Try
                    assem = System.Reflection.Assembly.LoadFile(DLL名称)
                Catch ex1 As Exception
                    Try
                        assem = System.Reflection.Assembly.Load(DLL名称)
                    Catch ex2 As Exception
                        bLoad = False
                    End Try
                End Try
            End Try

            If bLoad = True AndAlso assem IsNot Nothing Then
                Dim frm As Windows.Forms.Form = assem.CreateInstance(DLL类)
                If frm IsNot Nothing Then
                    FormShowBefore(frm)
                    If 打开模式 = 0 Then
                        Using frm
                            frm.ShowDialog(Me)
                        End Using
                    Else
                        frm.Show(Me)
                    End If
                End If
            End If

        End Sub

        '重写次类填充类数据
        Public Overridable Sub FormShowBefore(ByRef frm As Windows.Forms.Form)

        End Sub

        Private Sub tvMenu_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvMenu.NodeMouseDoubleClick

            NodeMouseDoubleClickImpl(e.Node)

        End Sub

    End Class
End Namespace

